<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<style>
			.input-file{position: absolute; left: 0px; top: 0px; display: inline-block; width: 100%; height: 30px; line-height: 30px; opacity: 0;}
		</style>
		<div class="super-theme-example storehouse_storehouse_page">
			<div class="easyui-layout" style="width:100%;height:100%;">
				<div id="p" data-options="region:'west',collapsible:false" title="仓库" style="width:15%;padding:10px">
					<ul id="storehouse-tree"></ul>
				</div>
				<div data-options="region:'center',collapsible:false" title="库存" style="width:84%;padding:10px">
					<div class="options-toolbar">
						<form id="inventoryDatagridQueryParams">
							<input class="easyui-textbox" name="productSkuBarcode" data-options="width:250,prompt:'商品SKU编码'">
							<a href="javascript:void(0)" class="easyui-linkbutton primary" data-options="iconCls:'fa fa-search'" onclick="storehouse.methods.queryDatagrid();">查询</a>
						</form>
						<div class="top-options">
							<a href="javascript:void(0)" class="easyui-menubutton info" data-options="menu:'#storehouse_list_tools'">操作</a>
							<div id="storehouse_list_tools" style="width:120px;">
								<div data-options="iconCls:'fa fa-download'" onclick="exportExcel(commonConfig.mgUrlPrefix() + '/admin/storehouse/inventory/export' , storehouse.methods.initDatagridQueryParams())">导出</div>
								<div class="menu-sep"></div>
								<div data-options="iconCls:'fa fa-upload'">
									<span class="input-span">库存盘点</span>
									<input class="input-file" id="storehouse_list_change_inventory_input_file" type="file" onchange="storehouse.methods.reSetStorehouse(this)" />
								</div>
								<div data-options="iconCls:'fa fa-file-excel-o'">
									<a class="download-link" download="" mce_href="#" href="excel/仓库盘点表.xlsx">下载仓库盘点表</a>
								</div>
							</div>
						</div>
					</div>
					<div id="inventory-datagrid-container">
						<table id="inventory-datagrid"></table>
					</div>
				</div>
			</div>
		</div>
		<script type="text/javascript" src="dists/super/js/xlsx.full.min.js" ></script>
		<script type="text/javascript">
			var storehouse = {
				datas : {
				},
				methods: {
					initStorehouseTree: function(){
						$('#storehouse-tree').tree({
						    url: commonConfig.mgUrlPrefix() + '/admin/storehouse/tree-list',
						    method: 'GET',
						    loadFilter: function(data,parent){
								if(data && data.code == 1){
									var treeRoot = {'text' : '全部仓库'};
									if(data.datas.length == 0){
										return [treeRoot];
									}else{
										var _treeData = [] ; 
										$.each(data.datas, function(index , treeData) {
											var _tmpData = {
											 		id: treeData.storehouseId,
											 		text: treeData.storehouseName,
											 		attributes: {
											 			comments: treeData.comments,
											 			isDefault: treeData.isDefault,
											 			isClear: treeData.isClear,
											 			currency: treeData.currency
											 		}
											};
											 _treeData.push(_tmpData);
											if(treeData.children && treeData.children.length > 0){
												$.each(treeData.children, function(index2 , treeData2) {
													_treeData.push(
														{
															id: treeData2.storehouseId,
													 		text: treeData2.storehouseName,
													 		attributes: {
													 			comments: treeData2.comments,
													 			isDefault: treeData2.isDefault,
													 			isClear: treeData2.isClear,
													 			currency: treeData2.currency
													 		}
														}
													);
												});
											}
										});
										treeRoot.children = _treeData ; 
										return [treeRoot] ; 
									}
								}
								return [] ; 
							},
							formatter: function(node){
								if(!node.attributes) return node.text;
								return node.text + ((node.attributes.isDefault == 1) ? '<i class="success">默认</i>' : (node.attributes.isClear == 1) ? '<i class="info">出清</i>' : '');
							},
						    onSelect: function(node){
						    	storehouse.methods.queryDatagrid();
						    }
						});
					},
					initInventoryDatagrid: function(){
						//设定height
						$('#inventory-datagrid-container').height($('.storehouse_storehouse_page').height() - $('.storehouse_storehouse_page .options-toolbar').height() - 30);
						$('#inventory-datagrid').datagrid({
						    url: commonConfig.mgUrlPrefix() + '/admin/storehouse/inventory',
						    fit: true,
							pagination: true,
						    columns:[[
								{field:'productName',title:'商品名称',width:100,align:'center'},
								{field:'productSkuBarcode',title:'商品编码',width:100,align:'center'},
								{field:'productSkuName',title:'商品规格',width:100,align:'center'},
								{field:'productSkuImage',title:'商品图片',width:80,align:'center',formatter:function(value,row,index){return '<img class="td-img" src="'+ value +'">';}},
								{field:'inventory',title:'库存量',width:60,align:'center'},
								{field:'comments',title:'备注',width:150,align:'center'}
						    ]]
						});
					},
					reSetStorehouse: function(e){
						var files = $(e).context.files
						var fileReader = new FileReader()
						if (files.length) {
							fileReader.onload = function (ev) {
								try {
									var workbook = XLSX.read(ev.target.result, {type: 'binary'});
								} catch (e) {
									$.messager.alert('提示',e,'error');
									return
								}
								// 创建收货单提交
								storehouse.methods.reSetStorehouseNext(XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]));
								e.value = '' // 清空
							}
							fileReader.readAsBinaryString(files[0]);
						}
					},
					reSetStorehouseNext: function(paramList){
						if(paramList.length == 0){
							showMessage('上传内容为空。。。')
							return false;
						}
						var _storehouseId = null
						var storehouseTree = $('#storehouse-tree').tree('getSelected') ; 
						if(storehouseTree && storehouseTree.id){
							_storehouseId = storehouseTree.id ;
						}
						if(_storehouseId == null){
							showMessage('请选择仓库后再操作。。。')
							return false;
						}
						var list = [] // 处理后的数据
						paramList.map((e)=>{
							list.push({
								productSkuBarcode: e['商品编码'], 
								storehouseId: _storehouseId, 
								quantity: e['真实库存'], 
								comments: e['备注']
							})
						})
						requestBodyServer(commonConfig.mgUrlPrefix() + '/admin/storehouse/inventory/edit' , 'PUT', list).then((response) => {
							if (response.code == 1) {
								if(response.datas && response.datas.length > 0){
									showErrorMsgs(response.datas , [[{field:'productSkuBarcode',title:'商品编码',width:60,align:'center'},{field:'errorMsg',title:'错误信息',width:60,align:'center'}]]);
								}else{
									showMessage('<i class="fa fa-info-circle"></i>&nbsp; 请求成功...');
									$('#inventory-datagrid').datagrid('reload');
								}
							}else {
								$.messager.alert('提示',response.message,'error');
							}
						}).catch((error) => {
							console.log(error)
						});
					},
					initDatagridQueryParams: function(){
						var params = $('#inventoryDatagridQueryParams').serializeArray();
						var queryParams = {};
						$.each(params , function(index , param){
						     queryParams[param.name] = param.value;
						})
						var storehouseTree = $('#storehouse-tree').tree('getSelected') ; 
						if(storehouseTree && storehouseTree.id){
							queryParams['storehouseId'] = storehouseTree.id ;
						}
						return queryParams;
					},
					queryDatagrid: function(){
						$('#inventory-datagrid').datagrid('reload',storehouse.methods.initDatagridQueryParams());
					}
				},
				init : function(){
					storehouse.methods.initStorehouseTree();
					storehouse.methods.initInventoryDatagrid();
				}
			}
			storehouse.init();
		</script>
	</body>
</html>
